/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import pikes.html.AbstractHtmlPage;
import pikes.xml.XMLTag;

/**
 * XHTML web page. With this class, one can use statically typed Java classes to enforce XHTML rules. The result is a
 * strict doc typed XHTML web page with 'html','head','title' and empty 'body' tags. The user of this class can set the
 * following properties: <code>
 * <ul>
 *  <li><code>docType</code> of type {@link XHTMLDocType}.</li>
 * </ul>
 * </code> <br>
 * The implementation of this class can override
 * <ul>
 * <li> {@link XHTMLPage#buildHeader(Head, Map, HttpServletRequest)} method to build the header of the page. </li>
 * <li> {@link XHTMLPage#buildBody(Body, Map, HttpServletRequest)} method to build the body of the page. </li>
 * <li> {@link XHTMLPage#decorateHtml(Html, Map, HttpServletRequest)} method to decorate the html tag. </li>
 * <li> {@link XHTMLPage#getTitle()} method to provide the title for the page. Default is empty string. </li>
 * </ul>
 * @author Peter Bona
 * @since 0.0.1
 */
public class XHTMLPage extends AbstractHtmlPage {

	final protected XMLTag createRootHtmlTag(Map model, HttpServletRequest request) throws Exception {
		Head head = new Head(new Title(getTitle(model, request)));
		buildHeader(head, model, request);

		Body body = new Body();
		buildBody(body, model, request);

		Html html = new Html(head, body);

		decorateHtml(html, model, request);

		return html.createTag();

	}

	/**
	 * Override this method to decorate the html tag of the page, for example setting id, dir or language.
	 * @param html
	 * @param model
	 * @param request
	 * @throws Exception
	 */
	protected void decorateHtml(Html html, Map model, HttpServletRequest request) throws Exception {
		// left intentionally empty, by default it does nothing
	}

	/**
	 * Override this method to build the body of the page.
	 * @param body
	 * @param model
	 * @param request
	 * @throws Exception
	 */
	protected void buildBody(Body body, Map model, HttpServletRequest request) throws Exception {
		// left intentionally empty, by default it does nothing
	}

	/**
	 * Override this method to add link, meta, script, etc elements under header.
	 * @param head
	 * @param model
	 * @param request
	 * @throws Exception
	 */
	protected void buildHeader(Head head, Map model, HttpServletRequest request) throws Exception {
		// left intentionally empty, by default it does nothing
	}

	/**
	 * Override this to add title to the web page.
	 * @return empty string
	 */
	protected CharSequence getTitle(Map model, HttpServletRequest request) throws Exception {
		return "";
	}
}
